######################################################################################
######################################################################################
#Replication code for models presented in:

#Buntaine, Mark T.
#Accoutability in Global Governance: Civil Society Claims for Environmental Performance at the World Bank
#International Studies Quarterly, forthcoming.

#Mark Buntaine contact (as of January 2014): buntaine@bren.ucsb.edu / 919.627.7066

#Current for R Version 3.0.2 (version "Frisbee Sailing")
######################################################################################
######################################################################################

####################################################################################
###Setting Up
####################################################################################

library(arm) #Version 1.6-10
library(MatchIt) #Version 2.4-21
library(mice) #Version 2.18

#Next step: load replication data file as object "rep.data" according to appropriate file pathway

#Data subsets
data.IPelig <- subset(rep.data,IP.eligible==1)
data.no.ibrd.only.prev3 <- subset(rep.data,bin.IBRD.only.prev3==0)
data.yes.ibrd.only.prev3 <- subset(rep.data,bin.IBRD.only.prev3==1)

#This function remakes the data frame with only the specified columns & NAs removed:
make <- function(data,names) {
  datasub <- as.data.frame(data)
  sub = substitute(names)
  names <- as.character(sub)
  data.make <- subset(datasub, select=names(datasub) %in% names)
  data.make <<- na.omit(data.make)
  data.make <- na.omit(data.make)
  print(c("ob =",nrow(data.make)),quote=F)}

####################################################################################
###Table 2
####################################################################################

data.mod1a <- na.omit(subset(data.IPelig, select=c("country","bin.IP.request", "ENGOs", "FH.polrights.repress","govt.eff","bin.sg.failure","n.A.prev5","lag.n.IP.ENVprob")))
mod1a <- glm(bin.IP.request ~  ENGOs + FH.polrights.repress + govt.eff + bin.sg.failure + n.A.prev5 + lag.n.IP.ENVprob, family=binomial, data=data.mod1a)
summary(mod1a)

data.mod1b.imp <- subset(data.IPelig, select=c("country","bin.IP.request", "ENGOs", "FH.polrights.repress","govt.eff","bin.sg.failure","n.A.prev5","lag.n.IP.ENVprob","urbanpop.pct","GDP.pc.2000","forest.mm2","reg.quality","voice.acct","n.proj.prev5"))
imp.1b <- mice(data.mod1b.imp, seed=102)
m1b.imp <- pool(glm.mids(bin.IP.request ~  ENGOs + FH.polrights.repress + govt.eff + bin.sg.failure + n.A.prev5 + lag.n.IP.ENVprob, family=binomial, data=imp.1b))
summary(m1b.imp)
##Diagnostic plots for the imputed datasets, all visual diagnostics look reasonable
densityplot(imp.1b,~ENGOs) #Note: similar density to observed values
bwplot(imp.1b) #Note: similar range to observed values
stripplot(imp.1b) #Note: none of imputed variables different from observed values

data.mod1c <- na.omit(subset(data.IPelig, select=c("country","bin.IP.request", "ENGOs", "govt.eff","bin.sg.failure","n.A.prev5","lag.n.IP.ENVprob")))
mod1c <- glm(bin.IP.request ~  ENGOs + govt.eff + bin.sg.failure + n.A.prev5 + lag.n.IP.ENVprob, family=binomial, data=data.mod1c)
summary(mod1c)

data.mod1d <- na.omit(subset(data.IPelig, select=c("country","bin.IP.request", "FH.polrights.repress","govt.eff","bin.sg.failure","n.A.prev5","lag.n.IP.ENVprob")))
mod1d <- glm(bin.IP.request ~ FH.polrights.repress + govt.eff + bin.sg.failure + n.A.prev5 + lag.n.IP.ENVprob, family=binomial, data=data.mod1d)
summary(mod1d)

####################################################################################
###Table 3
#################################################################################### 

###IBRD Only Eligible requests (Model 2a)
make(data.yes.ibrd.only.prev3,c(country,year,bin.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
m.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(m.out,interactions=T,standardize=T)
m.data<-match.data(m.out)
mod2a <- glm(bin.A ~ bin.request.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data, weights=weights)
summary(mod2a)
#Note: all treatment observations retained in matching

###IBRD Only Ineligible requests (Model 2b)
make(data.no.ibrd.only.prev3,c(country,year,bin.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
m.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(m.out,interactions=T,standardize=T)
m.data<-match.data(m.out)
mod2b <- glm(bin.A ~ bin.request.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data, weights=weights)
summary(mod2b)

###IBRD Only Eligible investigations (Model 3a)
make(data.yes.ibrd.only.prev3,c(country,year,bin.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
m.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=10000, discard="both", data=data.make)
summary(m.out,interactions=T,standardize=T)
m.data<-match.data(m.out)
mod3a <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data, weights=weights)
summary(mod3a)

###IBRD Only Ineligible investigations (Model 3b)
make(data.no.ibrd.only.prev3,c(country,year,bin.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
m.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=10000, discard="both", data=data.make)
summary(m.out,interactions=T,standardize=T)
m.data<-match.data(m.out)
mod3b <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data, weights=weights)
summary(mod3b)

####################################################################################
###Figures
####################################################################################
#Note: must set working directory for this code to work

###Prediction Model1b (ISQ R&R Figure 1a/b)

#Data values used for prediction
n.A.range <- seq(from=0, to=33, by=1)
ENGOs.25 <- rep(2,length(n.A.range))
ENGOs.median <-rep(median(data.IPelig$ENGOs,na.rm=T),length(n.A.range))
ENGOs.75 <- rep(12,length(n.A.range))
FH.polrights.repress.yes <- rep(1,length(n.A.range))
FH.polrights.repress.no <- rep(0,length(n.A.range))
GovtEff.mean <- rep(mean(data.IPelig$govt.eff, na.rm=T),length(n.A.range))
binSg.no <- rep(0,length(n.A.range))
ENVprob.no <- rep(0,length(n.A.range))

#Parameter values used for prediction from pooled fit
int <- rnorm(1000, mean=-4.55856437, sd=0.31104342)
ENGOs <- rnorm(1000, mean=0.03722959, sd=0.01060390)
FH.polrights.repress <- rnorm(1000, mean=-0.69571722, sd=0.42496685)
govt.eff <- rnorm(1000, mean=-0.51006282, sd=0.30739051)
bin.sg.failure <- rnorm(1000, mean=-0.29462417, sd=0.97267520)
n.A.prev5 <- rnorm(1000, mean=0.07736769, sd=0.03171482)
lag.n.IP.ENVprob <- rnorm(1000, mean=0.71624387, sd=0.38502773)

###ENGOs prediction
predict.75 <- invlogit(-4.55856437 + 0.03722959*ENGOs.75 - 0.69571722*FH.polrights.repress.no - 0.51006282*GovtEff.mean - 0.29462417*binSg.no + 0.07736769*n.A.range + 0.71624387*ENVprob.no)
predict.25 <- invlogit(-4.55856437 + 0.03722959*ENGOs.25 - 0.69571722*FH.polrights.repress.no - 0.51006282*GovtEff.mean - 0.29462417*binSg.no + 0.07736769*n.A.range + 0.71624387*ENVprob.no)

tiff("Mod1b_ENGOs_final.tiff", height=8, width=6, units="in", res=150)
layout(matrix(1:2, ncol = 1), widths = 1, heights = c(2,1.5), respect = FALSE)
par(mar = c(1, 6.1, 1, 2.1))
plot(n.A.range, predict.75, type="l", col="black", lwd=5, ylim=c(0,0.22), xlim=c(0,33), xaxt = 'n', ylab="Probability of Inspection Request", cex.lab=1.2, cex.axis=1.2)
lines(n.A.range, predict.25, type="l", col="light grey", lwd=5)
legend(x=0,y=0.15,legend=c("75% Percentile ENGOs (12)","25% Percentile ENGOs (2)"),fill=c("black","light grey"))

par(mar = c(4.1, 6.1, 0, 2.1))
plot(1,type="n",xlim=c(0,33),ylim=c(-25,100), ylab="Percent Change (90% CI)",xlab="Number of category A in previous 5 years", cex.lab=1.2, cex.axis=1.2)
abline(h=0, lty=2)

L1 <- length(n.A.range)
CI.frame <- as.data.frame(n.A.range)
for (i in 1:L1){
  sim.out1 <- invlogit(int + ENGOs*ENGOs.75[i] + FH.polrights.repress*FH.polrights.repress.no[i] + govt.eff*GovtEff.mean[i] + bin.sg.failure*binSg.no[i] + n.A.prev5*n.A.range[i] + lag.n.IP.ENVprob*ENVprob.no[i])
  sim.out2 <- invlogit(int + ENGOs*ENGOs.25[i] + FH.polrights.repress*FH.polrights.repress.no[i] + govt.eff*GovtEff.mean[i] + bin.sg.failure*binSg.no[i] + n.A.prev5*n.A.range[i] + lag.n.IP.ENVprob*ENVprob.no[i])
  percent.diff <- ((sim.out1-sim.out2)*100)/sim.out2
  percent.diff.sort <- sort(percent.diff)
  CI.frame$low[i] <- percent.diff.sort[50]
  CI.frame$mean[i] <- mean(percent.diff.sort)
  CI.frame$high[i] <- percent.diff.sort[950]}
polygon(x=c(CI.frame$n.A.range[1],CI.frame$n.A.range[1:34],CI.frame$n.A.range[34:2]),y=c(CI.frame$high[1],CI.frame$low[1:34],CI.frame$high[34:2]),col="light gray", border=NA)
lines(n.A.range,CI.frame$mean,type="l", col="black", lwd=3)
dev.off()

###Political repression prediction
predict.yes <- invlogit(-4.55856437 + 0.03722959*ENGOs.median - 0.69571722*FH.polrights.repress.yes - 0.51006282*GovtEff.mean - 0.29462417*binSg.no + 0.07736769*n.A.range + 0.71624387*ENVprob.no)
predict.no <- invlogit(-4.55856437 + 0.03722959*ENGOs.median - 0.69571722*FH.polrights.repress.no - 0.51006282*GovtEff.mean - 0.29462417*binSg.no + 0.07736769*n.A.range + 0.71624387*ENVprob.no)

tiff("Mod1b_repression_final.tiff", height=8 ,width=6, units="in", res=150)
layout(matrix(1:2, ncol = 1), widths = 1, heights = c(2,1.5), respect = FALSE)
par(mar = c(1, 6.1, 1, 2.1))
plot(n.A.range, predict.yes, type="l", col="black", lwd=5, ylim=c(0,0.22), xlim=c(0,33), xaxt = 'n', ylab="Probability of Inspection Request", cex.lab=1.2, cex.axis=1.2)
lines(n.A.range, predict.no, type="l", col="light grey", lwd=5)
legend(x=0,y=0.15,legend=c("No Political Repression","Political Repression"),fill=c("light grey","black"))

par(mar = c(4.1, 6.1, 0, 2.1))
plot(1,type="n",xlim=c(0,33),ylim=c(-100,10), ylab="Percent Change (90% CI)",xlab="Number of category A in previous 5 years", cex.lab=1.2, cex.axis=1.2)

L1 <- length(n.A.range)
CI.frame <- as.data.frame(n.A.range)
for (i in 1:L1){
  sim.out1 <- invlogit(int + ENGOs*ENGOs.median[i] + FH.polrights.repress*FH.polrights.repress.yes[i] + govt.eff*GovtEff.mean[i] + bin.sg.failure*binSg.no[i] + n.A.prev5*n.A.range[i] + lag.n.IP.ENVprob*ENVprob.no[i])
  sim.out2 <- invlogit(int + ENGOs*ENGOs.median[i] + FH.polrights.repress*FH.polrights.repress.no[i] + govt.eff*GovtEff.mean[i] + bin.sg.failure*binSg.no[i] + n.A.prev5*n.A.range[i] + lag.n.IP.ENVprob*ENVprob.no[i])
  percent.diff <- ((sim.out1-sim.out2)*100)/sim.out2
  percent.diff.sort <- sort(percent.diff)
  CI.frame$low[i] <- percent.diff.sort[50]
  CI.frame$mean[i] <- mean(percent.diff.sort)
  CI.frame$high[i] <- percent.diff.sort[950]}
polygon(x=c(CI.frame$n.A.range[1],CI.frame$n.A.range[1:34],CI.frame$n.A.range[34:2]),y=c(CI.frame$high[1],CI.frame$low[1:34],CI.frame$high[34:2]),col="light gray", border=NA)
lines(n.A.range,CI.frame$mean,type="l", col="black", lwd=3)
abline(h=0, lty=2)
dev.off()

###Prediction Figure of Model3b (ISQ R&R)
tiff("Mod3b_fitted_final.tiff", height=8 ,width=6, units="in", res=150)
layout(matrix(1:2, ncol = 1), widths = 1, heights = c(2,1.2), respect = FALSE)

par(mar = c(1, 6.1, 1, 2.1))
nA.range <- seq(from=0, to=5, by=1)
investigate.no <- rep(0,length(nA.range))
investigate.yes <-rep(1,length(nA.range))
sg.success <- rep(0,length(nA.range))
FHpol.mean <- rep(4.037,length(nA.range))
amount.bil <- rep(0.189954,length(nA.range))

prmat.yes <- predict(mod3b, data.frame(bin.investigate.prev3=investigate.yes, n.sg.success=sg.success, FH.polrights=FHpol.mean, n.A.prev3=nA.range, amount.prev3.bil=amount.bil), type="response")
prmat.no <- predict(mod3b, data.frame(bin.investigate.prev3=investigate.no, n.sg.success=sg.success, FH.polrights=FHpol.mean, n.A.prev3=nA.range, amount.prev3.bil=amount.bil), type="response")

plot(spline(nA.range, prmat.yes, method="n", n=201), type="l", col="black", lwd=5, ylim=c(0,1), xlim=c(0,5), xaxt = 'n', ylab="Probability of at least one\n category A project", cex.lab=1.2, cex.axis=1.2)
lines(spline(nA.range, prmat.no, method="n", n=201), type="l", col="light grey", lwd=5)
legend(x=0,y=1,legend=c("No Investigation","Investigation"),fill=c("light grey","black"),cex=1.2)

par(mar = c(4.1, 6.1, 0, 2.1))
sim.gate <- sim(mod3b,n.sims=1000)
coef <- coef(sim.gate)
plot(1,type="n",xlim=c(0,5),ylim=c(-110,20), ylab="% Difference CI",xlab="Number of category A in previous 3 years", cex.lab=1.2)
abline(h=0, lty=2)

L1 <- length(nA.range)
CI.frame <- as.data.frame(nA.range)
for (i in 1:L1){
  sim.out1 <- invlogit(coef[,1] + coef[,2]*investigate.yes[i] + coef[,3]*0 + coef[,4]*FHpol.mean[i] + coef[,5]*nA.range[i] + coef[,6]*amount.bil[i])
  sim.out2 <- invlogit(coef[,1] + coef[,2]*investigate.no[i] + coef[,3]*0 + coef[,4]*FHpol.mean[i] + coef[,5]*nA.range[i] + coef[,6]*amount.bil[i])
  percent.diff <- ((sim.out1-sim.out2)*100)/sim.out2
  percent.diff.sort <- sort(percent.diff)
  CI.frame$low[i] <- percent.diff.sort[50]
  CI.frame$mean[i] <- mean(percent.diff.sort)
  CI.frame$high[i] <- percent.diff.sort[950]}
polygon(x=c(CI.frame$nA.range[1:6],CI.frame$nA.range[6:1]),y=c(CI.frame$high[1:6],CI.frame$low[6:1]),col="light gray", border=NA)
lines(spline(nA.range,CI.frame$mean,method="n", n=201),type="l", col="black", lwd=5)
dev.off()

####################################################################################
###Table S1 (online technical appendix only)
#################################################################################### 

##With ENGOs
make(data.yes.ibrd.only.prev3,c(country,year,bin.A,bin.request.prev3,n.sg.success,n.sg.failure,ENGOs,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t4.2a.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(t4.2a.out,interactions=T,standardize=T)
m.data.t4.2a<-match.data(t4.2a.out)
mod2a.t4 <- glm(bin.A ~ bin.request.prev3 + n.sg.success + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data.t4.2a, weights=weights)
summary(mod2a.t4)

##With ENGOs
make(data.no.ibrd.only.prev3,c(country,year,bin.A,bin.request.prev3,n.sg.success,n.sg.failure,ENGOs,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t4.2b.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(t4.2b.out,interactions=T,standardize=T)
m.data.t4.2b<-match.data(t4.2b.out)
mod2b.t4 <- glm(bin.A ~ bin.request.prev3 + n.sg.success + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data.t4.2b, weights=weights)
summary(mod2b.t4)

##With ENGOs
make(data.yes.ibrd.only.prev3,c(country,year,bin.A,bin.investigate.prev3,n.sg.success,n.sg.failure,ENGOs,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t4.3a.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=10000, discard="both", data=data.make)
summary(t4.3a.out,interactions=T,standardize=T)
m.data.t4.3a<-match.data(t4.3a.out)
mod3a.t4 <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data.t4.3a, weights=weights)
summary(mod3a.t4)

##With ENGOs
make(data.no.ibrd.only.prev3,c(country,year,bin.A,bin.investigate.prev3,n.sg.success,n.sg.failure,ENGOs,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t4.3b.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=1000, discard="both", data=data.make)
summary(t4.3b.out,interactions=T,standardize=T)
m.data.t4.3b<-match.data(t4.3b.out)
mod3b.t4 <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + ENGOs + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=m.data.t4.3b, weights=weights)
summary(mod3b.t4)

####################################################################################
###Table S2 (online technical appendix only)
#################################################################################### 

##Count model
make(data.yes.ibrd.only.prev3,c(country,year,n.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t5.2a.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(t5.2a.out,interactions=T,standardize=T)
m.data.t5.2a<-match.data(t5.2a.out)
mod2a.t5 <- glm(n.A ~ bin.request.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=quasipoisson, data=m.data.t5.2a, weights=weights)
summary(mod2a.t5)

##Count model
make(data.no.ibrd.only.prev3,c(country,year,n.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t5.2b.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=5000, discard="both", data=data.make)
summary(t5.2b.out,interactions=T,standardize=T)
m.data.t5.2b<-match.data(t5.2b.out)
mod2b.t5 <- glm(n.A ~ bin.request.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=quasipoisson, data=m.data.t5.2b, weights=weights)
summary(mod2b.t5)

##Count model
make(data.yes.ibrd.only.prev3,c(country,year,n.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t5.3a.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=10000, discard="both", data=data.make)
summary(t5.3a.out,interactions=T,standardize=T)
m.data.t5.3a<-match.data(t5.3a.out)
mod3a.t5 <- glm(n.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=quasipoisson, data=m.data.t5.3a, weights=weights)
summary(mod3a.t5)

##Count model
make(data.no.ibrd.only.prev3,c(country,year,n.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,n.A.prev3,amount.prev3.bil))
#Genetic matching
t5.3b.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, method="genetic", int.seed = 102, unif.seed = 102, pop.size=10000, discard="both", data=data.make)
summary(t5.3b.out,interactions=T,standardize=T)
m.data.t5.3b<-match.data(t5.3b.out)
mod3b.t5 <- glm(n.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + n.A.prev3 + amount.prev3.bil, family=quasipoisson, data=m.data.t5.3b, weights=weights)
summary(mod3b.t5)

####################################################################################
###Table S3 (online technical appendix only)
####################################################################################

##Proportion model
make(data.yes.ibrd.only.prev3,c(country,year,prop.amount.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,prop.amount.A.prev3,bin.proj.prev3))
data.make <- subset(data.make,bin.proj.prev3>0)
#Genetic matching
t6.2a.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + prop.amount.A.prev3, method="genetic", int.seed = 102, unif.seed = 102, pop.size=1000, discard="both", data=data.make)
summary(t6.2a.out,interactions=T,standardize=T)
m.data.t6.2a<-match.data(t6.2a.out)
mod2a.t6 <- glm(prop.amount.A ~ bin.request.prev3 + n.sg.success + FH.polrights + prop.amount.A.prev3, family=binomial, data=m.data.t6.2a, weights=weights)
summary(mod2a.t6)

##Proportion model
make(data.no.ibrd.only.prev3,c(country,year,prop.amount.A,bin.request.prev3,n.sg.success,n.sg.failure,FH.polrights,prop.amount.A.prev3,bin.proj.prev3))
data.make <- subset(data.make,bin.proj.prev3>0)
#Genetic matching
t6.2b.out <- matchit(bin.request.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + prop.amount.A.prev3, method="genetic", int.seed = 102, unif.seed = 102, pop.size=1000, discard="both", data=data.make)
summary(t6.2b.out,interactions=T,standardize=T)
m.data.t6.2b<-match.data(t6.2b.out)
mod2b.t6 <- glm(prop.amount.A ~ bin.request.prev3 + n.sg.success + FH.polrights + prop.amount.A.prev3, family=binomial, data=m.data.t6.2b, weights=weights)
summary(mod2b.t6)

#Proportion model
make(data.yes.ibrd.only.prev3,c(country,year,prop.amount.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,prop.amount.A.prev3,bin.proj.prev3))
data.make <- subset(data.make,bin.proj.prev3>0)
#Genetic matching
t6.3a.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + prop.amount.A.prev3, method="genetic", int.seed = 102, unif.seed = 102, pop.size=1000, discard="both", data=data.make)
summary(t6.3a.out,interactions=T,standardize=T)
m.data.t6.3a<-match.data(t6.3a.out)
mod3a.t6 <- glm(prop.amount.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + prop.amount.A.prev3, family=binomial, data=m.data.t6.3a, weights=weights)
summary(mod3a.t6)

#Proportion model
make(data.no.ibrd.only.prev3,c(country,year,prop.amount.A,bin.investigate.prev3,n.sg.success,n.sg.failure,FH.polrights,prop.amount.A.prev3,bin.proj.prev3))
data.make <- subset(data.make,bin.proj.prev3>0)
#Genetic matching
t6.3b.out <- matchit(bin.investigate.prev3 ~ n.sg.success + n.sg.failure + FH.polrights + prop.amount.A.prev3, method="genetic", int.seed = 102, unif.seed = 102, pop.size=1000, discard="both", data=data.make)
summary(t6.3b.out,interactions=T,standardize=T)
m.data.t6.3b<-match.data(t6.3b.out)
mod3b.t6 <- glm(prop.amount.A ~ bin.investigate.prev3 + n.sg.success + FH.polrights + prop.amount.A.prev3, family=binomial, data=m.data.t6.3b, weights=weights)
summary(mod3b.t6)

####################################################################################
###Table S4 (online technical appendix only)
####################################################################################

##No Matching
mod2a.t7 <- glm(bin.A ~ bin.request.prev3 + n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=data.yes.ibrd.only.prev3)
summary(mod2a.t7)

##No Matching
mod2b.t7 <- glm(bin.A ~ bin.request.prev3 + n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=data.no.ibrd.only.prev3)
summary(mod2b.t7)

##No Matching
mod3a.t7 <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=data.yes.ibrd.only.prev3)
summary(mod3a.t7)

##No Matching
mod3b.t7 <- glm(bin.A ~ bin.investigate.prev3 + n.sg.success + n.sg.failure + FH.polrights + n.A.prev3 + amount.prev3.bil, family=binomial, data=data.no.ibrd.only.prev3)
summary(mod3b.t7)